<?php
/*
 * Copyright notice
 * 
 * (c) 2009 MH-Dev.de, Martin Hesse <info@mh-dev.de>
 * 
 * This script is part of the MH-Dev.-Board project. The MH-Dev.-Board 
 * is free software; you can redistribute it and/or modify it under the 
 * terms of the GNU General Public License as published by 
 * the Free Software Foundation; either version 3 of the License, 
 * or (at your option) any later version.
 * 
 * This program is distributed in the hope that it will be useful, 
 * but WITHOUT ANY WARRANTY; without even the implied warranty 
 * of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. 
 * See the GNU General Public License for more details.
 * 
 * You should have received a copy of the GNU 
 * General Public License along with this program; if not, 
 * see http://www.gnu.org/licenses/.
 */


/**
 * Backend GroupsController
 *
 * @author	Martin Hesse <info@mh-dev.de>
 * @package	MH-Dev.-Board
 */
class Backend_GroupsController extends Zend_Controller_Action
{
	private $registry; // registry
	private $auth; // user-values
	private $settings; // board-settings
	private $baseUrl; // helper
	
	/*
	 * Init class vars and stuff
	 */
	public function init()
	{		
		// registry
		$this->registry = Zend_Registry::getInstance();
		
		// settings
		$this->settings	= $this->registry->get('settings');
		
		// helper
		$baseUrl		= new MHDEV_View_Helper_BaseUrl();
		$this->baseUrl	= $baseUrl->BaseUrl();
		
		// auth
		$auth = Zend_Auth::getInstance();
		$this->view->auth = $auth->hasIdentity();
    	
		if($auth->hasIdentity())
		{
			$this->view->auth_role 			= $auth->getIdentity()->role;
			$this->view->username 			= $auth->getIdentity()->username;
			$this->view->auth_uid			= $auth->getIdentity()->uid;
			$this->view->unread_messages	= $auth->getIdentity()->count_unread_messages;
		}
		else
		{
			$this->view->username 	= $this->registry->get('lang')->_('guest');
		}

		$this->auth = $auth;
    	
		// lang
		$this->view->translate = $this->registry->get('lang');
		
		// include js
		$this->view->headScript()->appendFile($this->baseUrl . '/static/js/jquery-1.3.2.min.js');
		$this->view->headScript()->appendFile($this->baseUrl . '/static/js/backend.js');
	}
	
	
	
	/*
	 * List groups
	 */
	public function indexAction()
	{
		// db
		$groups			= new Groups();
		
		// query
		$getGroups		= $groups->fetchAll();

		// views
		$this->view->count		= $getGroups->count();
		$this->view->results	= $getGroups->toArray();
	}
	
	
	
	/*
	 * Add a new group
	 * 
	 * @param null
	 * 
	 * @return void
	 */
	public function addAction()
	{
		// vars
		$formData		= $this->_request->getPost();
		
		// db
		$groups			= new Groups();
		$permission 	= new Permission();
		
		// form
		$form			= new GroupsForm();
		$form->removeElement('is_deleted');
		
		if ($this->_request->isPost() && $form->isValid($formData))
		{
			$form->removeElement('submit');
			
			if($uid = $groups->insert($form->getValues()))
			{
				$this->_redirect('/backend/group-' . $uid . '.html');
			}
		}
		else
		{
			// query
			$queryPerm	= $permission->fetchAll();
			
			// fill select-fields
			$selPerm = $form->getElement('perm_uid');
			if($queryPerm->count() > 0)
			{
				foreach($queryPerm->toArray() AS $result)
				{
					$selPerm->addMultiOption($result['uid'], $result['title']);
				}
			}
			
			$form->populate($formData);
		}

		$this->view->form = $form;
	}
	
	
	
	/*
	 * Edit a group
	 * 
	 * @param null
	 * 
	 * @return void
	 */
	public function editAction()
	{
		// vars
		$group_uid		= $this->_request->getParam('uid', 0);
		$formData 		= $this->_request->getPost();
		
		// db
		$groups		= new Groups();
		$permission	= new Permission();

		// form
		$form		= new GroupsForm();
		
		if($group_uid)
		{
			$getGroup	= $groups->find($group_uid);
			$resGroup	= $getGroup->toArray();
			
			$queryPerms	= $permission->fetchAll();
			
			if($getGroup->count() > 0)
			{
				
				// fill select-fields
				$selPerm = $form->getElement('perm_uid');
				
				if($queryPerms->count() > 0)
				{
					foreach($queryPerms->toArray() AS $result)
					{
						$selPerm->addMultiOption($result['uid'], $result['title']);
					}
				}
				
				$form->populate($resGroup[0]);
			}
		}
		
		if ($this->_request->isPost() && $form->isValid($formData))
		{
			if($formData['is_deleted'] == 1)
			{
				$groups->delete('uid=' . $group_uid);
				$this->_redirect('/backend/groups.html');
			}
			
			$form->removeElement('submit');
			$form->removeElement('is_deleted');
			
			if($groups->update($form->getValues(), 'uid=' . $group_uid))
			{
				$this->_redirect('/backend/group-' . $group_uid . '.html');
			}
		}
		
		$this->view->form = $form;
	}
}
?>